package com.jhf.skypac_pda.db.impl;

import android.content.Context;
import android.util.Log;

import com.elvishew.xlog.XLog;
import com.jhf.skypac_pda.db.DBManager;
import com.jhf.skypac_pda.db.bean.DB_OperationRecord;
import com.jhf.skypac_pda.db.gen.DB_OperationRecordDao;
import com.jhf.skypac_pda.db.gen.DaoMaster;
import com.jhf.skypac_pda.db.gen.DaoSession;
import com.jhf.skypac_pda.utils.TimeUtils;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OperationRecordDaoImp {
    private static OperationRecordDaoImp instance;
    private DB_OperationRecordDao dao;


    private OperationRecordDaoImp(Context context) {
        DaoSession daoSession = DBManager.getInstance(context).getDaoSession(context);
        dao = daoSession.getDB_OperationRecordDao();
    }

    public static synchronized OperationRecordDaoImp getInstance(Context context) {
        if (instance == null)
            instance = new OperationRecordDaoImp(context);
        return instance;
    }

    public static void init() {
        instance = null;
    }

    public boolean isCodeCompleted(String materialTankFeedingOrderCode) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).build();

        List<DB_OperationRecord> beans = query.list();
        if (beans == null || beans.size() == 0) {
            return false;
        } else {
            if (beans.get(beans.size()-1).getStockLocationCode() == null) {
                return false;
            }
        }
        return true;
    }

    public boolean isHaveNotStartedOrder(String materialTankFeedingOrderCode) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).build();

        List<DB_OperationRecord> beans = query.list();
        if (beans == null || beans.size() == 0) {
            return false;
        } else {
            for (int i = 0; i < beans.size(); i++) {
                if (beans.get(i).getMaterialTankFeedingRecordStatusDesc().equals("未开始")) {
                    return true;
                } else {
                    continue;
                }
            }
        }
        return false;
    }

    public boolean isHaveStartingOrder(String materialTankFeedingOrderCode) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).build();

        List<DB_OperationRecord> beans = query.list();
        if (beans == null || beans.size() == 0) {
            return false;
        } else {
            for (int i = 0; i < beans.size(); i++) {
                if (beans.get(i).getMaterialTankFeedingRecordStatusDesc().equals("进行中")) {
                    return true;
                } else {
                    continue;
                }
            }
        }
        return false;
    }

    public boolean isMaterialCode(String materialTankFeedingOrderCode, String code) {

        boolean isFull = false;

        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).build();

        List<DB_OperationRecord> beans = query.list();
        /*DB_OperationRecord bean = (beans == null || beans.size() == 0) ? null
                : beans.get(0);*/
        if (beans == null || beans.size() == 0) {
            return true;
        } else {
            for (int i = 0; i < beans.size(); i++) {
                if (beans.get(i).getMaterialCode() != null && beans.get(i).getStockLocationCode() != null) {
                    if (i == beans.size()-1) {
                        isFull = true;
                    }

                    if (isFull) {
                        return true;
                    }
                    continue;
                }

                if (beans.get(i).getMaterialCode() == null) {
                    return true;
                } else if (beans.get(i).getStockLocationCode() == null) {
                    return false;
                }
            }
        }

        //XLog.i("扫码结果data = %s,type=%s", "理论上这里是永远走不到的");
        Log.i("扫码结果data = %s,type=%s", "理论上这里是永远走不到的");
        return true;
    }

    public void AddCodeAndTime(String materialTankFeedingOrderCode, String code) {

        boolean isFull = false;

        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).build();

        List<DB_OperationRecord> beans = query.list();
        /*DB_OperationRecord bean = (beans == null || beans.size() == 0) ? null
                : beans.get(0);*/
        if (beans == null || beans.size() == 0) {
            DB_OperationRecord bean = new DB_OperationRecord();
            bean.setMaterialTankFeedingOrderCode(materialTankFeedingOrderCode);
            bean.setMaterialCode(code);
            bean.setScanPalletCodeTime(TimeUtils.getStringDate(new Date()));
            bean.setMaterialTankFeedingRecordStatusDesc("未开始");
            dao.insert(bean);
        } else {
            for (int i = 0; i < beans.size(); i++) {
                if (beans.get(i).getMaterialCode() != null && beans.get(i).getStockLocationCode() != null) {
                    if (i == beans.size()-1) {
                        isFull = true;
                    }

                    if (isFull) {
                        DB_OperationRecord bean = new DB_OperationRecord();
                        bean.setMaterialTankFeedingOrderCode(materialTankFeedingOrderCode);
                        bean.setMaterialCode(code);
                        bean.setScanPalletCodeTime(TimeUtils.getStringDate(new Date()));
                        bean.setMaterialTankFeedingRecordStatusDesc("未开始");
                        dao.insert(bean);
                    }
                    continue;
                }

                if (beans.get(i).getMaterialCode() == null) {
                    DB_OperationRecord bean = new DB_OperationRecord();
                    bean.setMaterialTankFeedingOrderCode(materialTankFeedingOrderCode);
                    bean.setMaterialCode(code);
                    bean.setScanPalletCodeTime(TimeUtils.getStringDate(new Date()));
                    bean.setMaterialTankFeedingRecordStatusDesc("未开始");
                    dao.insert(bean);
                    return;
                } else if (beans.get(i).getStockLocationCode() == null) {
                    beans.get(i).setStockLocationCode(code);
                    beans.get(i).setScanLocationCodeTime(TimeUtils.getStringDate(new Date()));
                    beans.get(i).setMaterialTankFeedingRecordStatusDesc("未开始");
                    dao.update(beans.get(i));
                }
            }
        }
    }

    public ArrayList<DB_OperationRecord> getRecordsById(String materialTankFeedingOrderCode) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).orderDesc(DB_OperationRecordDao.Properties.Id)
                .build();
        ArrayList<DB_OperationRecord> infos = (ArrayList)query.list();
        if (infos == null || infos.size() == 0) return null;
        return infos;
    }

    public DB_OperationRecord getRecordById(String materialTankFeedingOrderCode) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode))
                .build();
        ArrayList<DB_OperationRecord> infos = (ArrayList)query.list();
        if (infos == null || infos.size() == 0) return null;
        //if (infos.get(infos.size()-1).getMaterialCode() == null || infos.get(infos.size()-1).getStockLocationCode() == null) return null;
        return infos.get(infos.size()-1);
    }

    public void deleteAll() {
        dao.deleteAll();
    }

    public void deleteRecordById(String materialTankFeedingOrderCode) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode)).build();

        ArrayList<DB_OperationRecord> infos = (ArrayList)query.list();
        if (infos == null || infos.size() == 0) return;
        for (int i = 0; i < infos.size(); i++) {
            dao.delete(infos.get(i));
        }
    }

    public void deleteRecordById(long id) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.
                where(DB_OperationRecordDao.Properties.Id.eq(id)).build();

        ArrayList<DB_OperationRecord> infos = (ArrayList)query.list();
        if (infos == null || infos.size() == 0) return;
        dao.delete(infos.get(0));
    }

    public void upDateStatus(Context context, String materialTankFeedingOrderCode, String status) {
        QueryBuilder<DB_OperationRecord> queryBuilder = dao.queryBuilder();
        Query<DB_OperationRecord> query = queryBuilder.where(DB_OperationRecordDao.Properties.MaterialTankFeedingOrderCode.eq(materialTankFeedingOrderCode))
                .build();
        ArrayList<DB_OperationRecord> infos = (ArrayList)query.list();
        if (infos == null || infos.size() == 0) return;
        for (int i = 0; i < infos.size(); i++) {
            Log.i("数据库操作=====", i + "之前infos数据状态"  + infos.get(i).getMaterialTankFeedingRecordStatusDesc());
            if (!infos.get(i).getMaterialTankFeedingRecordStatusDesc().contains("完成")) {
                Log.i("数据库操作=====", i + "操作了");
                DB_OperationRecord db_operationRecord = dao.loadByRowId(infos.get(i).getId());
                db_operationRecord.setMaterialTankFeedingRecordStatusDesc(status);
                dao.updateInTx(db_operationRecord);
            }
        }
    }

    public void saveDate(ArrayList<DB_OperationRecord> db_operationRecords) {
        for (DB_OperationRecord db_operationRecord: db_operationRecords) {
            DB_OperationRecord bean = new DB_OperationRecord();
            bean.setMaterialTankFeedingOrderCode(db_operationRecord.getMaterialTankFeedingOrderCode());
            bean.setMaterialCode(db_operationRecord.getMaterialCode());
            bean.setScanPalletCodeTime(db_operationRecord.getScanPalletCodeTime());
            bean.setMaterialTankFeedingRecordStatusDesc(db_operationRecord.getMaterialTankFeedingRecordStatusDesc());
            bean.setStockLocationCode(db_operationRecord.getStockLocationCode());
            bean.setScanLocationCodeTime(db_operationRecord.getScanLocationCodeTime());
            dao.insert(bean);
        }
    }
}
